package annuaire.tools;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import annuaire.config.MailConfigLoader;

public class MailSender {

	public static final String DEFAULT_SUBJECT = "Aucun Sujet";
	public static final String HEADER_XMAILER = "MyApp Integrated Mailer";

	private MailSender() {
	}

	private static Session getSession() {
		final Properties prop = new Properties();
		prop.setProperty("mail.smtp.host", MailConfigLoader
				.getParam("smtpServer"));
		prop.setProperty("mail.transport.protocol", "smtp");
		prop.setProperty("mail.mime.charset", MimeUtility
				.mimeCharset(MailConfigLoader.getParam("charset")));
		prop.setProperty("mail.debug", MailConfigLoader.getParam("debugMode"));

		Session session = Session.getDefaultInstance(prop);

		return session;
	}

	private static MimeMessage createMessage(Session session, String from,
			String replyTos, String tos, String ccs, String bccs, String subject)
			throws MessagingException {

		if (from == null || from.equals("")) {
			throw new IllegalArgumentException(
					"the \"from\" field cannot be empty");
		}
		if (tos == null || tos.equals("")) {
			throw new IllegalArgumentException(
					"the \"to\" field cannot be empty");
		}

		MimeMessage message = new MimeMessage(session);
		message.setFrom(new InternetAddress(from));
		message.setRecipients(Message.RecipientType.TO, InternetAddress
				.parse(tos));

		if (replyTos == null || replyTos.equals("")) {
			message.setReplyTo(InternetAddress.parse(from));
		} else {
			message.setReplyTo(InternetAddress.parse(replyTos));
		}
		if (ccs != null && !ccs.equals("")) {
			message.setRecipients(Message.RecipientType.CC, InternetAddress
					.parse(ccs));
		}
		if (bccs != null && !bccs.equals("")) {
			message.setRecipients(Message.RecipientType.BCC, InternetAddress
					.parse(bccs));
		}
		if (subject == null || subject.equals("")) {
			message.setSubject(MailSender.DEFAULT_SUBJECT, MailConfigLoader
					.getParam("charset"));
		} else {
			message.setSubject(subject, MailConfigLoader.getParam("charset"));
		}

		message.setContentLanguage(new String[] { "fr" });

		message.setHeader("X-Mailer", MailSender.HEADER_XMAILER);
		message.setSentDate(new Date());

		return message;
	}

	private static void sendMessage(Message _message) throws MessagingException {
		_message.saveChanges();

		Transport.send(_message);
	}

	public static void sendMail(String from, String replyTos, String tos,
			String ccs, String bccs, String subject, String text) {

		try {

			MimeMessage message = MailSender.createMessage(MailSender
					.getSession(), from, replyTos, tos, ccs, bccs, subject);

			if (text == null || text.equals("")) {
				message.setText("", MailConfigLoader.getParam("charset"));
			} else {
				message.setText(text, MailConfigLoader.getParam("charset"));
			}

			message.setHeader("Content-Type", "text/plain; charset="
					+ MailConfigLoader.getParam("charset"));

			MailSender.sendMessage(message);

		} catch (MessagingException excp) {
			throw new RuntimeException(
					"an error occured while sending message", excp);
		}
	}

	public static void sendSimpleMail(String from, String to, String subject,
			String text) {

		MailSender.sendMail(from, null, to, null, null, subject, text);

	}
}
